package cn.com.ctcloud.saas.ids.demo.user.webtier;

import java.util.List;

import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.com.ctcloud.saas.core.annotation.UserTokenCheck;
import cn.com.ctcloud.saas.core.pojo.ApiResponse;
import cn.com.ctcloud.saas.core.utils.Log;
import cn.com.ctcloud.saas.ids.demo.user.domain.UserDemoObj;
import cn.com.ctcloud.saas.ids.demo.user.service.IUserDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Title: ChinaCT IDS
 * <p>
 * Description: Demo Restfull
 * <p>
 * Copyright: Copyright (c) 2008
 * <p>
 * Company: Liaoning Open Data Communication Co.,Ltd.
 *
 * @author lihui
 * @version 2.0.0
 */
@CrossOrigin // 使用CORS技术解决跨域问题
@RestController // 相当于@Controller+@ResponseBody两个注解的结合，返回json数据不需要在方法前面加@ResponseBody注解了，但使用@RestController这个注解，就不能返回jsp,html页面，视图解析器无法解析jsp,html页面
@RequestMapping("/api/user")
@Api(value = "user-restful", description = "基于Restful的用户接口")
public class UserRestDemoController {
	@Autowired
	IUserDemoService userService;

	@ApiOperation("Token验证")
	@UserTokenCheck //在需要验证token的方法上添加此注解
	@RequestMapping(value = "/token", method = RequestMethod.GET)
	public ApiResponse<String> checkToken() {
		return new ApiResponse<String>(HttpStatus.SC_OK, "OK", null);
	}

	/**
	 * 访问地址：http://localhost:9090/api/user/list
	 *
	 * @return 用户集合
	 */
	@ApiOperation("查询用户集合")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public List<UserDemoObj> getUsers() {
		Log.info("查询用户集合");
		return userService.findAll();
	}

	/**
	 * 访问地址：http://localhost:9090/api/user/123456
	 *
	 * @param logicId
	 *            用户逻辑主键
	 * @return 用户信息
	 */
	@ApiOperation("通过逻辑主键查询用户")
	@ApiImplicitParams({ @ApiImplicitParam(name = "logicId", value = "用户逻辑主键", dataType = "string", required = true) })
	@RequestMapping(value = "/{logicId}", method = RequestMethod.GET)
	public UserDemoObj getByLogicId(@PathVariable("logicId") final String logicId) {
		Log.info("通过逻辑主键查询用户");
		return userService.getByLogicId(logicId);
	}

	/**
	 * 访问地址：http://localhost:9090/api/user/save
	 *
	 * @param name
	 *            登录名
	 * @param pwd
	 *            密码
	 * @return 是否成功
	 */
	@ApiOperation("保存用户基本信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userObj", value = "用户对象", dataType = "UserObj", required = true) })
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@RequestBody final UserDemoObj userObj) {
		Log.info("保存用户基本信息");
		userService.saveOrUpdate(userObj);
		return "true";
	}

	/**
	 * 访问地址：http://localhost:9090/api/user/update
	 *
	 * @param logicId
	 *            用户逻辑主键
	 * @param name
	 *            登录名
	 * @param pwd
	 *            密码
	 * @return 是否成功
	 */
	@ApiOperation("更新用户基本信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userObj", value = "用户对象", dataType = "UserObj", required = true) })
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public String update(@RequestBody final UserDemoObj userObj) {
		Log.info("更新用户基本信息");
		userService.saveOrUpdate(userObj);
		return "true";
	}

	/**
	 * 访问地址：http://localhost:9090/api/user/delete/123
	 *
	 * @param logicId
	 *            用户逻辑主键
	 * @return 是否成功
	 */
	@ApiOperation("通过逻辑主键删除用户")
	@ApiImplicitParams({ @ApiImplicitParam(name = "logicId", value = "用户逻辑主键", dataType = "string", required = true) })
	@RequestMapping(value = "/delete/{logicId}", method = RequestMethod.DELETE)
	public String restDelete(@PathVariable(value = "logicId") final String logicId) {
		Log.info("通过逻辑主键删除用户");
		userService.deleteByLogicId(logicId);
		return "true";
	}
}
